package com.ry.demo.concurrent.demo.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 */
public class ReentrantLockDemo {

    static int data = 0;
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();


    public static void main3 (String[] args) throws InterruptedException {


        condition.await();

        condition.signal();


    }

    public static void main(String[] args) throws InterruptedException {

        lock.lock();

//        lock.tryLock(3, TimeUnit.SECONDS);

        Thread.sleep(100);

        new Thread(()->{
            lock.lock();
        }).start();

        lock.unlock();


    }

    private void a(){


        ReentrantLockDemo reentractLockDemo = this;
        a();


    }

    public static void main2(String[] args) {
        new Thread() {

            public void run() {
                for(int i = 0; i < 10; i++) {
                    lock.lock();
                    ReentrantLockDemo.data++;
                    System.out.println(ReentrantLockDemo.data);
                    lock.unlock();
                }
            };

        }.start();

        new Thread() {

            public void run() {
                for(int i = 0; i < 10; i++) {
                    lock.lock();
                    ReentrantLockDemo.data++;
                    System.out.println(ReentrantLockDemo.data);
                    lock.unlock();
                }
            };

        }.start();
    }

}
